<?php
class clsXML {
	private $xml;
	private $tab = 1;
	private $arquivo;
	public static $fazer = true;
	private $renomear;
	public function __construct($version = '1.0', $encode = 'utf-8') {
		$this->renomear = new clsRenomear ();
		if (self::$fazer == true) {
			self::$fazer = false;
			
			header ( "Content-type: text/xml; charset=utf-8" );
			
			$xml .= "<?xml version=\"$version\" encoding=\"$encode\" ?>\n";
			$xml .= "<essystem xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
			
			$this->xml = $xml;
		}
	}
	public function openTag($name, $comentario = "", $tpcampo = "") {
		if ($comentario)
			$comentario = '<!-- ' . ($comentario) . ' --> ';
		
		if ($_GET ['exemplo'] == '')
			$comentario = "";
		
		if ($name != "") {
			$this->addTab ();
			$this->xml .= "<$name> $comentario\n";
			$this->tab ++;
		}
	}
	public function closeTag($name) {
		if ($name != '') {
			$this->tab --;
			for($i = 0; $i < $this->tab; $i ++)
				$this->xml .= "\t";
			$this->xml .= "</$name>\n";
		}
	}
	private function addTab() {
		for($i = 0; $i < $this->tab; $i ++)
			$this->xml .= "\t";
	}
	public function addTag($name, $value, $comentario = "", $tpcampo = "") {
		if ($name == 'dtinc')
			$comentario = 'Data que foi cadastrado ' . $comentario . ', CAMPO (TIMESTAMP)';
		if ($name == 'dtalt')
			$comentario = 'Data que foi alterado ' . $comentario . ', CAMPO (TIMESTAMP)';
		if ($name == 'dtexc')
			$comentario = 'Data que foi exclu&iacute;do ' . $comentario . ', CAMPO (TIMESTAMP)';
		$name = trim ( $name );
		for($i = 0; $i < $this->tab; $i ++)
			$this->xml .= "\t";
		
		if ($comentario) {
			if ($tpcampo)
				$comentario = $comentario . ', CAMPO(' . strtoupper ( $tpcampo ) . ')';
			$comentario = '<!-- ' . ($comentario) . ' --> ';
		}
		if ($_GET ['exemplo'] == '')
			$comentario = "";
		else
			$value = "";
		
		$tpconfig = connectorFactory::query ( 'select * from tpconfig' );
		
		if ($name != 'email' && $name != 'dtnasc' && $name != 'fone' && $name != 'cel') {
			$removerAcento = $tpconfig [0] [valor];
			$carEsp = $tpconfig [1] [valor];
		}
		
		$value = $this->renomear->retirarAcento ( $value, $removerAcento, $carEsp );
		
		if ($name == 'dtexc' && ($value == '0000-00-00 00:00:00' || $value == ''))
			$this->xml .= "<$name>" . html_entity_decode ( utf8_encode ( trim ( '0000-00-01 00:00:01' ) ) ) . "</$name> $comentario\n";
		else
			$this->xml .= "<$name>" . html_entity_decode ( utf8_encode ( trim ( $value ) ) ) . "</$name> $comentario\n";
	}
	public function conteudo($value) {
		for($i = 0; $i < $this->tab; $i ++)
			$this->xml .= "\t";
		$this->xml .= $value;
	}
	public function __toString() {
		$this->xml .= "</essystem>";
		// $fp = fopen ( str_replace ( '.xml', '', $this->arquivo ) . '.xml',
		// "w+" );
		// $escreve = fwrite ( $fp, $this->xml );
		return $this->xml;
	}
}
?>